package com.alex.common;

import org.apache.log4j.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Random;

/**
 * @ClassName: MD5Util
 * @Description: MD5加密工具类
 * @author zephyr
 * @date 2016年5月5日 上午9:15:56
 *
 */
public final class MD5Util {

	private static Logger logger = Logger.getLogger(MD5Util.class);

	/**
	 * 获取32位MD5代码
	 * */
	public static String get32Code(String text) {
		StringBuffer buf = new StringBuffer("");
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(text.getBytes());
			byte bytes[] = md.digest();

			int base;
			for (int offset = 0; offset < bytes.length; offset++) {
				base = bytes[offset];
				if (base < 0)
					base += 256;
				if (base < 16)
					buf.append("0");
				buf.append(Integer.toHexString(base));
			}
		} catch (NoSuchAlgorithmException e) {
			logger.info("MD5加密失败");
		}

		return buf.toString(); //32位的加密
	}


	public static  String getMisi32Code(String text){
	    return  get32Code(text+"***###misi###***").toUpperCase();
    }



	/**
	 * 获取16位MD5代码
	 * */
	public static String get16Code(String text) {
		return get32Code(text).substring(8, 24); //16位的加密;
	}


	public static Object get16Code(){
		long base = new Date().getTime();
		String code = base+""+GetNum(99998)+CommonUtil.getUUID();
		code = get16Code(code);

		return code;
	}


	private static String GetNum(int number){
		Random random = new Random();
		int num =random.nextInt(number)+1;
		int num_len = (number+"").length();
		String _num = String.format("%0"+(num_len)+"d", num);
		return _num;
	}

	public static void main(String[] args) {

//		List<String> list = new ArrayList<>();
//		int aa = 0;
//		for (int i=0;i<1000;i++){
//			String code = get16Code().toString();
//			if (list.contains(code)){
//				aa ++;
//				continue;
//			}
//			list.add(code);
//		}
//		System.out.println(list.toString());
//		System.out.println(aa);







		//System.out.println(DateUtils.getDayBeginOrEnd(DateUtils.addDay(new Date(), 0), "begin"));

	}


}
